%-------------------------------------------------------------------------
% Sub routine: Find start and end observation and construct estimation data
%
% Estimation Codes for State-Level Results in  
% Ben-David, Itzhak, Sebastian Weber, and Pascal Towbin 
% "Inferring Expectations from Observables: Evidence from the Housing Market" 
% Review of Economics and Statistics
% ------------------------------------------------------------------------ 

% find start and end point of data in each state
indic_mm_full=[Vacancy(1+t*(ct-1):t*ct)];
first_obs = t*(ct-1) + find(sum((isnan(indic_mm_full)),2),1, 'last')+1+grdiff;
start=first_obs;
ende=ct*t;
n_step = ende-first_obs;         % Periods to Display for IRF
gapstart=n_step;
infl_start=find(sum((isnan(indic_mm_full)),2),1, 'last')+1+grdiff;

%% Define vector of dependent variables
v_price     = (log(HPI(start:ende)./cpi_regio(start:ende))-log(HPI(start-grdiff:ende-grdiff)./cpi_regio(start-grdiff:ende-grdiff)))*100; 
v_permit    = log(1+totpermit(start:ende)*100);
v_int       = Interestrate(start:ende)-expinf_yearly(infl_start:t,2);
v_vac       = Vacancy(start:ende);
v_pti       = (log(HPI(start:ende)./(median_inc(start:ende)/median_inc(start))))*100;
v_year      = Year(start:ende);

y=  [v_pti  v_price v_permit v_int v_vac];


%% Labels of shocks and variables
series = {'Price to Income', 'House price ','Permits','Mortgage rate','Vacancy rate'};
series_transg4={'no trans', 'Annual Growth',};
shockname   = {'Housing','Housing consumption','Mortgage','Expectation';
            'supply shock','shock','rate shock','shock'};
shockname2  = {'Housing supply shock','Housing consumption shock', 'Mortgage rate shock','Expectation shock'};
varname     = {'Price','House','Permits','Mortgage','Vacancy';
              'to Income','price level','level', 'rate', 'rate'};
colorcode=[0 0.5 0; 0 0 1; 0 1 1; 1 0 0;0.4 0.4 0.4;];

%% Variable order
hous_start  = 1;
n_pti       = hous_start;
n_price_n   = hous_start+1;          
n_inv_l     = hous_start+2;    
n_mrate     = hous_start+3;       
n_total_vac = hous_start+4;
[T,n_var] = size(y);
n_pr_cum    = n_var+1;          
    
%Which Variables should be cumulated for the Historical Decomp 
cum=[n_price_n ];

